//
//  CrowdfundSubView.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/7.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class CrowdfundSubView: UIView {

    private lazy var imageView : UIImageView = {
        let imageView = UIImageView()
        self.addSubview(imageView)
        return imageView
    }()
    
    var model:HomeItemModel?{
        
        didSet{
            
            guard let item = self.model else { return  }
            
            self.titleLabel.text = item.item?.name ?? ""
            self.subtitleLabel.text = item.item?.summary ?? ""
            self.imageView.kf.setImage(with: URL(string: item.item?.pic_url ?? ""))
            self.priceLabel.text = "¥\((item.item?.price_min ?? 0)/100)"
            self.percentLabel.text = "\(String(describing: item.item!.progress))%"
            self.progressLabel.text = "\(item.item?.saled_count ?? 0)/\(item.item?.target_count ?? 0)"
            self.progressView.progress = 0.5
        }
    }
    
    
    var location:NSInteger = 0{
        
        didSet
        {
            if self.location == 0 {
                
                self.backgroundColor = RGB(r: 251, g: 244, b: 237)
                self.titleLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(15)
                    make.right.lessThanOrEqualTo(-10)
                    make.top.equalTo(20)
                    make.height.equalTo(20)
                }
                
                self.subtitleLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(15)
                    make.width.equalTo(kScreenW/2)
                    make.top.equalTo(self.titleLabel.snp_bottom)
                    make.height.equalTo(15)
                }
                
                self.imageView.snp.makeConstraints { (make) in
                    make.top.equalTo(0)
                    make.right.equalTo(-15)
                    make.size.equalTo(CGSize(width: 140*kHeightRatio - 20, height: 140*kHeightRatio - 20))
                }
                
                self.priceLabel.snp.makeConstraints { (make) in
                    
                    make.left.equalTo(15)
                    make.top.equalTo(self.subtitleLabel.snp_bottom)
                    
                }
                
                self.progressView.snp.makeConstraints { (make) in
                    
                    make.left.right.equalToSuperview()
                    make.height.equalTo(5)
                    make.bottom.equalTo(-3)
                    
                }
                
                
                self.percentLabel.snp.makeConstraints { (make) in
                    
                    make.right.equalTo(-15)
                    make.centerY.equalTo(self.progressLabel.snp_centerY)
                }
                
                self.progressLabel.snp.makeConstraints { (make) in
                    
                    make.left.equalTo(15)
                    make.bottom.equalTo(self.progressView.snp_top).offset(-5)
                }
                
                self.tagLabel.snp.makeConstraints { (make) in
                    make.centerY.equalTo(self.progressLabel.snp_centerY)
                    make.left.equalTo(self.progressLabel.snp_right).offset(10)
                    make.height.equalTo(18)
                    make.width.equalTo(18)
                }
                
                
            }
            else if self.location == 1 {
                
                self.backgroundColor = RGB(r: 252, g: 247, b: 253)
                self.titleLabel.snp.makeConstraints { (make) in
                    make.left.equalTo(15)
                    make.right.lessThanOrEqualTo(-10)
                    make.top.equalTo(5)
                    make.height.equalTo(20)
                }
                self.priceLabel.snp.makeConstraints { (make) in
                    
                    make.left.equalTo(15)
                    make.top.equalTo(self.titleLabel.snp_bottom)
                    make.height.equalTo(15)
                }
                
                self.imageView.snp.makeConstraints { (make) in
                    make.top.equalTo(self.titleLabel.snp_bottom).offset(5)
                    make.right.equalTo(-15)
                    make.size.equalTo(CGSize(width: 80*kHeightRatio , height: 80*kHeightRatio))
                }
                
                self.progressView.snp.makeConstraints { (make) in
                    
                    make.left.right.equalToSuperview()
                    make.height.equalTo(5)
                    make.bottom.equalTo(-6)
                }
                self.percentLabel.snp.makeConstraints { (make) in
                    
                    make.right.equalTo(-15)
                    make.centerY.equalTo(self.progressLabel.snp_centerY)
                }
                
                self.progressLabel.snp.makeConstraints { (make) in
                    
                    make.left.equalTo(15)
                    make.bottom.equalTo(self.progressView.snp_top).offset(-5)
                }
                
                self.tagLabel.snp.makeConstraints { (make) in
                    make.centerY.equalTo(self.progressLabel.snp_centerY)
                    make.left.equalTo(self.progressLabel.snp_right).offset(10)
                    make.height.equalTo(18)
                    make.width.equalTo(18)
                }
            }
        }
    }
    
    
    private lazy var titleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.hexColor(hexadecimal: "#333333")
   
        self.addSubview(label)
        return label
    }()
    
    private lazy var subtitleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.hexColor(hexadecimal: "#999999")
        label.textAlignment = NSTextAlignment.left
        self.addSubview(label)
        return label
    }()
    
    private lazy var priceLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.hexColor(hexadecimal: "#f62700")
   
        self.addSubview(label)
        return label
    }()
    
    private lazy var progressLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 11)
        label.textColor = UIColor.hexColor(hexadecimal: "#f98700")
        self.addSubview(label)
        return label
    }()
    
    private lazy var tagLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.backgroundColor = UIColor.hexColor(hexadecimal: "#f62700")
        label.textColor = .white
        label.text = "爆"
        label.textAlignment = .center
        self.addSubview(label)
        return label
    }()
    
    private lazy var percentLabel : UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.hexColor(hexadecimal: "#f62700")
        self.addSubview(label)
        return label
    }()
    
    private lazy var progressView : MQGradientProgressView = {
        let view = MQGradientProgressView(frame: CGRect(x: 0, y: 0, width: (self.location==0 ? kScreenW-20 : (kScreenW-20-3)/2), height: 5))
        view.colorArr = [UIColor.gray]
        view.bgProgressColor = UIColor.white
        view.progress = 0.35;
        self.addSubview(view)
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.hexColor(hexadecimal: "#f0edeb")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
